import 'dart:io';

import 'package:easy_rich_text/easy_rich_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/community_business/publish_page/widget/video_cover_select_widget.dart';
import 'package:tiktokadult/hj_page/community_business/publish_page/widget/video_cover_widget.dart';
import 'package:tiktokadult/hj_page/darkweb/widget/add_media_source_button.dart';
import 'package:tiktokadult/hj_utils/global.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';

import '../../../darkweb/widget/add_media_cover_button.dart';
import '../../../home_business/widget/support_up_alert.dart';
import '../chose_tag_page.dart';
import '../publish_controller.dart';

BoxDecoration get publishDecoration => BoxDecoration(color: Color(0x0DFFFFFF), borderRadius: BorderRadius.circular(10));

enum LinkType { topic, video, link }

Widget _buildImg(String path, double width, Function(String) deleteImgHandler) {
  return ClipRRect(
    borderRadius: BorderRadius.circular(8),
    child: Container(
      width: width,
      height: width,
      child: Stack(
        children: [
          Image.file(
            File(path),
            width: width,
            height: width,
            fit: BoxFit.cover,
          ),
          Positioned(
              right: 6,
              top: 6,
              child: GestureDetector(
                onTap: () => deleteImgHandler(path),
                child: Container(
                  width: 20,
                  height: 20,
                  decoration: BoxDecoration(color: Colors.white.withOpacity(.8), borderRadius: BorderRadius.circular(10)),
                  alignment: Alignment.center,
                  child: const Icon(
                    Icons.close,
                    size: 14,
                  ),
                ),
              ))
        ],
      ),
    ),
  );
}

extension LinkTitle on LinkType {
  String get getTitle {
    if (this == LinkType.topic) return '选择话题';
    if (this == LinkType.video) return '关联视频';
    return '磁力链接';
  }

  String getHint() {
    if (this == LinkType.video) return '影片或短片的番号';
    if (this == LinkType.link) return '添加磁力链接';
    return '';
  }

  String getBottomTitle() {
    if (this == LinkType.video) return '添加番号';
    if (this == LinkType.link) return '磁力链接';
    return '';
  }

  String get leadingIcon {
    if (this == LinkType.link) return 'publish_magnet_link';
    return 'topic_icon';
  }
}

// 提交视屏
class ChoseVideoWidget extends StatelessWidget {
  ChoseVideoWidget({super.key});

  final controller = Get.find<PublishController>();

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                '添加视频',
                style: TextStyle(color: Colors.white, fontSize: 16.sp, fontWeight: FontWeight.w500),
              ),
              10.sizeBoxW,
              Text(
                '最大300M以内',
                style: TextStyle(color: Color(0xff666666), fontSize: 12.sp),
              ),
            ],
          ),
          14.h.sizeBoxH,
          Obx(() {
            final widthCell = (MediaQuery.of(context).size.width - 52) / 3;
            return Row(children: [
              () {
                if (controller.video.value.customCover == null) {
                  return AddMediaCoverButton(
                    width: widthCell,
                    height: widthCell,
                    onTap: controller.choseCover,
                  );
                }
                return Container(
                  constraints: const BoxConstraints(maxWidth: 230, maxHeight: 219),
                  child: Stack(
                    children: [
                      VideoCoverSelectWidget(
                        controller.video.value.customCover ?? '',
                        // ratio: controller.video.value.ratio ?? 1,
                        width: widthCell, height: widthCell,
                        isLocal: true,
                      ),
                      Positioned(
                        left: 0,
                        right: 0,
                        bottom: 10,
                        child: GestureDetector(
                          child: Container(
                            margin: EdgeInsets.symmetric(horizontal: 25),
                            padding: EdgeInsets.symmetric(horizontal: 5, vertical: 3),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(borderRadius: BorderRadius.circular(6), color: Color(0xCC000000)),
                            child: Text(
                              "更换封面",
                              style: TextStyle(color: Color(0xFFFFFFFF)),
                            ),
                          ),
                          onTap: controller.choseCover,
                        ),
                      ),
                      Positioned(
                        right: 6,
                        top: 6,
                        child: GestureDetector(
                          child: Container(
                            padding: const EdgeInsets.all(
                              4,
                            ),
                            decoration: BoxDecoration(color: Colors.white.withOpacity(.8), borderRadius: BorderRadius.circular(10)),
                            child: const Icon(
                              Icons.close,
                              size: 12,
                              color: Color(0xff333333),
                            ),
                          ),
                          onTap: () => controller.deleteCover(),
                        ),
                      ),
                    ],
                  ),
                );
              }(),
              6.h.sizeBoxW,
              () {
                if (controller.video.value.localPath == null) {
                  return AddMediaSourceButton(
                    width: widthCell,
                    height: widthCell,
                    onTap: controller.choseVideo,
                    isVideo: true,
                  );
                }
                return Container(
                  constraints: const BoxConstraints(maxWidth: 230, maxHeight: 219),
                  child: Stack(
                    children: [
                      VideoCoverWidget(
                        controller.video.value.localCover ?? '',
                        // ratio: controller.video.value.ratio ?? 1,
                        width: widthCell,
                        height: widthCell,
                        isLocal: true,
                      ),
                      Positioned(
                        right: 6,
                        top: 6,
                        child: GestureDetector(
                          child: Container(
                            padding: const EdgeInsets.all(
                              4,
                            ),
                            decoration: BoxDecoration(color: Colors.white.withOpacity(.8), borderRadius: BorderRadius.circular(10)),
                            child: const Icon(
                              Icons.close,
                              size: 12,
                              color: Color(0xff333333),
                            ),
                          ),
                          onTap: () => controller.deleteSelectVideo(),
                        ),
                      )
                    ],
                  ),
                );
              }(),
            ]);
          })
        ],
      ),
    );
  }
}

// 发布页面相关ui
class TopicWidget extends StatelessWidget {
  final LinkType type;

  TopicWidget({super.key, this.type = LinkType.topic});

  final controller = Get.find<PublishController>();

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => GestureDetector(
        onTap: () async {
          final model = await Get.toNamed(ChoseTopicPage.routeName, arguments: {'tag': controller.type.tag, 'select': controller.topics});
          if (model is TagsBean) {
            controller.topics.clear();
            controller.topics.add(model);
          }
        },
        child: Container(
          margin: EdgeInsets.only(top: 12.h),
          padding: EdgeInsets.symmetric(horizontal: 14.w),
          alignment: Alignment.centerLeft,
          height: 60.h,
          decoration: publishDecoration,
          child: controller.topics.isEmpty
              ? Row(
                  children: [
                    // Image.asset("#_icon.webp".communityPath, width: 18),
                    // 8.sizeBoxW,
                    Text(
                      "#",
                      style: TextStyle(color: Color(0xff9C9DA3), fontSize: 24.sp, fontWeight: FontWeight.bold),
                    ),
                    10.sizeBoxW,
                    Text(
                      '选择标签',
                      style: TextStyle(color: Color(0xff9C9DA3), fontSize: 12.sp, fontWeight: FontWeight.w500),
                    ),
                    const Spacer(),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 16.sp,
                      color: Color(0xff868587),
                    )
                  ],
                )
              : _buildTopicInfo(context),
        ),
      ),
    );
  }

  Widget _buildTopicInfo(BuildContext context) {
    return GestureDetector(
      child: Row(
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: NetworkImageLoader(
              imageUrl: controller.topics.first.coverImg ?? '',
              width: 40,
              height: 40,
            ),
          ),
          5.sizeBoxW,
          Text(
            (controller.topics.first.name ?? ''),
            style: TextStyle(color: Colors.white, fontSize: 16.sp, fontWeight: FontWeight.w500),
          ),
          Spacer(),
          Icon(
            Icons.arrow_forward_ios,
            size: 16.sp,
            color: Color(0xff868587),
          )
        ],
      ),
      onTap: () => controller.topicNavigato(context, type),
    );
  }
}

// 输入标题
class InputTitleWidget extends StatelessWidget {
  InputTitleWidget({super.key});

  final controller = Get.find<PublishController>();

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 60.h,
      margin: EdgeInsets.only(top: 14.h),
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      alignment: Alignment.centerLeft,
      decoration: publishDecoration,
      child: TextField(
        maxLines: 1,
        textAlign: TextAlign.left,
        textAlignVertical: TextAlignVertical.center,
        style: TextStyle(color: Colors.white, fontSize: 16.sp, height: 22 / 15),
        focusNode: controller.titleFouceN,
        controller: controller.titleController,
        decoration: InputDecoration(
          border: InputBorder.none,
          hintText: '请填写标题',
          hintStyle: TextStyle(color: Color(0xff525252), fontSize: 12.sp, height: 22 / 15),
          contentPadding: EdgeInsets.zero,
          isDense: true,
        ),
      ),
    );
  }
}

class InputDescribeWidget extends StatelessWidget {
  InputDescribeWidget({super.key});

  final controller = Get.find<PublishController>();

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        controller.contentFouceN.requestFocus();
      },
      child: Container(
        // height: 111,
        constraints: BoxConstraints(minHeight: 171.h),
        margin: const EdgeInsets.only(top: 14),
        padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 11.h),
        decoration: publishDecoration,
        child: Stack(
          children: [
            TextField(
              style: TextStyle(color: Colors.white, fontSize: 16.sp),
              maxLength: 500,
              maxLines: null,
              focusNode: controller.contentFouceN,
              controller: controller.describeController,
              decoration: InputDecoration(
                  border: InputBorder.none,
                  hintStyle: TextStyle(color: Color(0xff525252), fontSize: 12.sp),
                  hintMaxLines: 10,
                  hintText: '有趣的介绍能让你的逼格提高N个档次！...',
                  counter: SizedBox(),
                  contentPadding: EdgeInsets.zero,
                  isDense: true),
            ),
            Positioned(
              child: ChangeNotifierProvider<TextEditingController>(
                create: (_) => controller.describeController,
                child: Consumer<TextEditingController>(
                  builder: (__, ctr, child) => Text(
                    '${ctr.text.length}/300',
                    style: TextStyle(
                      color: Color(0xff757575),
                      fontSize: 12,
                    ),
                  ),
                ),
              ),
              bottom: 0,
              right: 0,
            )
          ],
        ),
      ),
    );
  }
}

//图片选择
class ChoseImageWidget extends StatelessWidget {
  final String? hint;
  final String? highlightText;
  final Color? highlightTextColor;
  final double? highlightTextSize;

  ChoseImageWidget({super.key, this.hint, this.highlightText, this.highlightTextColor, this.highlightTextSize}) {
    controller = Get.find<PublishController>();
  }

  PublishController? controller;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 14),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Container(
          padding: EdgeInsets.only(bottom: 3),
          child: EasyRichText(
            hint ?? '添加图集 图片数量最多上传9张哦～',
            defaultStyle: TextStyle(color: Color(0xff666666), fontSize: 12.sp),
            patternList: [
              EasyRichTextPattern(
                targetString: highlightText ?? '',
                style:
                    TextStyle(color: highlightTextColor ?? Colors.white, fontSize: highlightTextSize ?? 16.sp, fontWeight: FontWeight.w500),
              )
            ],
          ),
        ),
        14.h.sizeBoxH,
        LayoutBuilder(builder: (__, constrains) {
          final width_ = (constrains.maxWidth - 16) / 3;
          final images = controller!.imgs;
          return Obx(
            () => Wrap(
              runSpacing: 8,
              spacing: 8,
              crossAxisAlignment: WrapCrossAlignment.start,
              children: images
                  .asMap()
                  .map((k, v) {
                    if (k == images.length - 1) {
                      if (k == 9) return MapEntry(k, SizedBox.shrink());
                      return MapEntry(
                        k,
                        AddMediaSourceButton(
                          isVideo: false,
                          onTap: controller!.choseImg,
                          height: width_,
                          width: width_,
                        ),
                      );
                    }
                    return MapEntry(k, _buildImg(images[k], width_, (path) => images.remove(path)));
                  })
                  .values
                  .toList(),
            ),
          );
        }),
      ]),
    );
  }

// Widget _buildImg(String path, double width) {
//   return ClipRRect(
//     borderRadius: BorderRadius.circular(8),
//     child: Stack(
//       children: [
//         Image.file(
//           File(path),
//           width: width,
//           height: width,
//           fit: BoxFit.cover,
//         ),
//         Positioned(
//             right: 6,
//             top: 6,
//             child: GestureDetector(
//               onTap: () => controller.imgs.remove(path),
//               child: Container(
//                 width: 20,
//                 height: 20,
//                 decoration: BoxDecoration(
//                     color: Colors.white.withOpacity(.8),
//                     borderRadius: BorderRadius.circular(10)),
//                 alignment: Alignment.center,
//                 child: const Icon(
//                   Icons.close,
//                   size: 14,
//                 ),
//               ),
//             ))
//       ],
//     ),
//   );
// }
}

// 选择地区
class SettingLocationWidget extends StatelessWidget {
  SettingLocationWidget({super.key});

  final controller = Get.find<PublishController>();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        margin: const EdgeInsets.only(top: 12),
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
        decoration: publishDecoration,
        child: Row(
          children: [
            SvgPicture.asset(
              'assets/images/nut/publish_location.svg',
              width: 16,
              height: 16,
            ),
            const SizedBox(
              width: 6,
            ),
            const Text(
              '选择地址',
              style: TextStyle(color: Color(0xff999999), fontSize: 14, fontWeight: FontWeight.w500),
            ),
            const Spacer(),
            // Obx(
            //   () => Text(
            //     controller.city.value?.name ?? '',
            //     style: TextStyle(
            //         color: controller.gold.value == null ? const Color(0xff999999) : const Color(0xffF25477),
            //         fontSize: 14,
            //         fontWeight: FontWeight.w500),
            //   ),
            // ),
            const SizedBox(
              width: 6,
            ),
            const Icon(
              Icons.arrow_forward_ios,
              size: 12,
            )
          ],
        ),
      ),
      onTap: () {
        // Get.bottomSheet(ChoseLocationBottomSheet());
      },
    );
  }
}

class ChoseTagsWidget extends StatelessWidget {
  ChoseTagsWidget({super.key});

  final controller = Get.find<PublishController>();

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 18),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                '选择标签',
                style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600),
              ),
              Spacer(),
              GestureDetector(
                onTap: () => Get.toNamed(
                  ChoseTopicPage.routeName,
                  arguments: {'tag': controller.type.tag, 'select': controller.topics},
                ),
                child: Text(
                  '更多',
                  style: TextStyle(color: Color(0xffe0e0e0), fontSize: 12),
                ),
              )
            ],
          ),
          12.sizeBoxH,
          Obx(
            () => Wrap(
              alignment: WrapAlignment.start,
              spacing: 14,
              runSpacing: 6,
              children: controller.topics.map((element) => _buildTopic(element)).toList(),
            ),
          ),
          18.sizeBoxH,
        ],
      ),
    );
  }

  Widget _buildTopic(TagsBean model) {
    return Stack(
      children: [
        Positioned(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 14, vertical: 4),
            margin: EdgeInsets.only(right: 7),
            decoration: BoxDecoration(color: Color(0xff212121), borderRadius: BorderRadius.circular(16)),
            child: Text(
              model.name ?? '',
              style: TextStyle(color: Color(0xffe0e0e0), fontSize: 14),
            ),
          ),
        ),
        Positioned(
          right: 0,
          top: 0,
          child: GestureDetector(
            onTap: () => controller.topics.remove(model),
            child: Image.asset(
              'close_grey_circle.png'.commonImgPath,
              width: 14,
              height: 14,
            ),
          ),
        )
      ],
    );
  }
}

// 设置金币
class SettingGoldWidget extends StatelessWidget {
  SettingGoldWidget({super.key});
  final controller = Get.find<PublishController>();
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: Container(
        margin: const EdgeInsets.only(top: 18),
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
        decoration: publishDecoration,
        child: Row(
          children: [
            Image.asset(
              'community_coin.webp'.communityPath,
              width: 20,
              height: 20,
            ),
            10.sizeBoxW,
            const Text(
              '设置价格',
              style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600),
            ),
            const Spacer(),
            Obx(
              () => Text(
                controller.gold.value == null ? '设置' : '${controller.gold.value} 金币',
                style: TextStyle(
                    // color: controller.gold.value == null ? Colors.white.withOpacity(.6) : const Color(0xff6954E7),
                    color: Color(0xffF68216),
                    fontSize: 12,
                    fontWeight: FontWeight.w400),
              ),
            ),
          ],
        ),
      ),
      onTap: () async {
        final res = await Get.bottomSheet(
          SupportUPAlert(
            isPublish: true,
            selectCoin: int.tryParse(controller.gold.value ?? ''),
          ),
        );
        controller.gold.value = res == null ? null : res.toString();
      },
    );
  }
  // SettingGoldWidget({super.key});
  //
  // final controller = Get.find<PublishController>();
  //
  // @override
  // Widget build(BuildContext context) {
  //   return Container(
  //     margin: const EdgeInsets.only(top: 14),
  //     height: 46,
  //     padding: EdgeInsets.symmetric(horizontal: 14.w),
  //     alignment: Alignment.centerLeft,
  //     decoration: publishDecoration,
  //     child: TextField(
  //       style: TextStyle(color: Colors.white, fontSize: 12.sp),
  //       onEditingComplete: () {},
  //       onChanged: (value) {
  //         controller.gold.value = value;
  //       },
  //       maxLength: 4,
  //       focusNode: controller.goldFouceN,
  //       inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[0-9]'))],
  //       decoration: InputDecoration(
  //         border: InputBorder.none,
  //         isDense: true,
  //         counterText: '',
  //         contentPadding: EdgeInsets.zero,
  //         hintText: '请设置金币',
  //         hintStyle: TextStyle(
  //           color: Color(0xff9C9DA3),
  //           fontSize: 12.sp,
  //           height: 1,
  //         ),
  //       ),
  //     ),
  //   );
  // }
}

// 相关alert 和 modal

class CommunityRulesAlert extends StatelessWidget {
  const CommunityRulesAlert({super.key});

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Center(
        child: Container(
          margin: const EdgeInsets.only(left: 32, right: 32),
          padding: EdgeInsets.only(left: 24, right: 24, top: 32, bottom: 24),
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(20)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                '社区规则',
                style: TextStyle(color: Color(0xff333333), fontSize: 24, fontWeight: FontWeight.w500),
              ),
              SizedBox(
                height: 12,
              ),
              Divider(
                height: .5,
                color: Colors.black.withOpacity(.05),
              ),
              12.sizeBoxH,
              Text(
                '''· 禁止发布广告图文 (含Line、WeChat、QQ、网址、二维码等外链)
 · 禁止发布诱导图文 (求网址、求软体等)
 · 禁止发布儿童色情、兽*内容
· 禁止发布交易相关、政治等内容''',
                style: TextStyle(color: Colors.black.withOpacity(.6), fontSize: 14, fontWeight: FontWeight.w400),
              ),
              12.sizeBoxH,
              Text(
                '请遵守社区规则，否则会被官方机器人拉黑',
                style: TextStyle(color: Color(0xff666666), fontSize: 12, fontWeight: FontWeight.w400),
              ),
              36.sizeBoxH,
              GestureDetector(
                child: Container(
                  height: 44,
                  // width: 205,
                  width: double.infinity,
                  margin: EdgeInsets.symmetric(horizontal: 16),
                  decoration: BoxDecoration(color: const Color(0xff6954E7), borderRadius: BorderRadius.circular(20)),
                  alignment: Alignment.center,
                  child: const Text(
                    '我知道了',
                    style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
                  ),
                ),
                onTap: () => Get.back(),
              ),
              // 24.sizeBoxH,
            ],
          ),
        ),
      ),
    );
  }
}

/// 选择地区

class SettingGoldAlert extends StatelessWidget {
  int multiple = 10; //倍数
  int leastCoin = 10; //最小金币数输入
  // PublishController? controller;
  int? gold;
  late ValueNotifier<int> notifier;
  final controller = TextEditingController();
  final focusNode = FocusNode();
  List<int>? golds;

  SettingGoldAlert({super.key, this.gold, this.multiple = 10, this.leastCoin = 10, this.golds}) {
    notifier = ValueNotifier(gold ?? 0);
    controller.addListener(() {
      final value = controller.text;
      // gold = int.tryParse(value);
      if (value.isNotEmpty) {
        notifier.value = 0;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        decoration: BoxDecoration(
          color: Color(0xff1F2031),
          borderRadius: BorderRadius.circular(8),
        ),
        child: _buildContent(),
      ),
    );
  }

  Widget _buildContent() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        18.sizeBoxH,
        const Text(
          '设置价格',
          style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500),
        ),
        18.sizeBoxH,
        Container(height: 1, color: Colors.white.withOpacity(0.05)),
        18.sizeBoxH,
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 39),
          child: LayoutBuilder(builder: (_, cons) {
            final width_ = (cons.maxWidth - 36) / 3;
            return ValueListenableBuilder(
              valueListenable: notifier,
              builder: (context, value, child) {
                return Wrap(
                  runSpacing: 12,
                  spacing: 12,
                  children: (golds ?? List.generate(8, (index) => (index + 1) * multiple)).map((e) {
                    final selet_ = value.toString() == e.toString();

                    return GestureDetector(
                        child: Container(
                          decoration: BoxDecoration(
                              color: Color(0xff16152A),
                              borderRadius: BorderRadius.circular(8),
                              border: Border.all(color: selet_ ? AppColors.primaryHighColor : Colors.transparent)),
                          width: width_,
                          height: 46,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Image.asset(
                                'community_coin.webp'.communityPath,
                                width: 22,
                                height: 22,
                              ),
                              const SizedBox(height: 6),
                              Text(
                                '$e',
                                style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w500),
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          focusNode.unfocus();
                          if (gold == e) {
                            gold = 0;
                            notifier.value = 0;
                          } else {
                            gold = e;
                            notifier.value = e;
                            controller.text = '';
                          }
                        });
                  }).toList(),
                );
              },
            );
          }),
        ),

        18.sizeBoxH,
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              '自定义',
              style: TextStyle(color: Color(0xff333333), fontSize: 14, fontWeight: FontWeight.w400),
            ),
            Container(
              height: 24,
              width: 47,
              margin: const EdgeInsets.symmetric(horizontal: 9),
              alignment: Alignment.center,
              decoration: BoxDecoration(color: const Color(0xffd9d9d9), borderRadius: BorderRadius.circular(5)),
              child: TextField(
                textAlign: TextAlign.center,
                maxLength: 4,
                controller: controller,
                focusNode: focusNode,
                style: const TextStyle(color: Color(0xff333333), fontSize: 12, height: 1),
                keyboardType: TextInputType.number,
                inputFormatters: [
                  FilteringTextInputFormatter.allow(RegExp("[0-9]")), //数字
                ],
                decoration: const InputDecoration(
                    helperText: '',
                    border: InputBorder.none,
                    hintText: '',
                    counterText: '',
                    contentPadding: EdgeInsets.zero,
                    isDense: false),
              ),
            ),
            const Text(
              '颗钻石',
              style: TextStyle(color: Color(0xff333333), fontSize: 14, fontWeight: FontWeight.w400),
            ),
          ],
        ),
        // ),
        18.sizeBoxH,

        GestureDetector(
            child: Container(
              height: 44,
              width: double.infinity,
              decoration: BoxDecoration(color: const Color(0xff6954E7), borderRadius: BorderRadius.circular(22)),
              alignment: Alignment.center,
              child: const Text(
                '确定设置',
                style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
              ),
            ),
            onTap: () {
              late int gold_;
              if (controller.text.isNotEmpty) {
                gold_ = int.tryParse(controller.text) ?? 0;
              } else {
                gold_ = notifier.value;
              }
              if ((gold_) < leastCoin) {
                showToast('至少$leastCoin钻石');
                return;
              }
              Get.back(result: gold_);
            }),
        18.sizeBoxH,
      ],
    );
  }
}

class BottomHeader extends StatelessWidget {
  final String centerTitle;
  final String? rightTitle;
  final Function()? action;

  BottomHeader(this.centerTitle, {this.rightTitle, this.action});

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            GestureDetector(
                child: const Icon(
                  Icons.clear_sharp,
                  size: 18,
                ),
                onTap: () => Get.back()),
            if (rightTitle != null)
              GestureDetector(
                  child: Text(
                    rightTitle ?? '',
                    style: const TextStyle(color: Color(0xff333333), fontSize: 14, fontWeight: FontWeight.w500),
                  ),
                  onTap: () => action?.call()),
          ],
        ),
        Text(
          centerTitle,
          style: const TextStyle(color: Color(0xff333333), fontSize: 14, fontWeight: FontWeight.w500),
        ),
      ],
    );
  }
}
